package com.example.beikeyigou;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.beikeyigou.goods.GoodsTypeFragment;
import com.example.beikeyigou.goods.TypeAdapter;
import com.example.beikeyigou.goods.TypeBean;
import com.example.beikeyigou.untils.BaseFragment;
import com.example.beikeyigou.untils.URLUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;


public class TypeFragment extends BaseFragment {

    private ListView typeLv;
    private EditText searchEt;//搜索框
    private ImageView searchIv;
    private List<TypeBean.DataBean> data;
    private TypeAdapter adapter;
    private FragmentManager fm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_type, container, false);

        initView(view);

        fm = getChildFragmentManager();

        data = new ArrayList<>();

        adapter = new TypeAdapter(getContext(),data);
        typeLv.setAdapter(adapter);

        getNetWork(URLUtils.queryAllCategory_url);
        setListener();
        //changeRightPage();
        return view;
    }


    //点击左侧item切换右侧页面
    private void changeRightPage(int pos) {
        TypeBean.DataBean dataBean = data.get(pos);
        int id = dataBean.getId();
        FragmentTransaction transaction = fm.beginTransaction();
        GoodsTypeFragment ftfrg = GoodsTypeFragment.newInstance(id + "", "");
        transaction.replace(R.id.frag_type_layout,ftfrg);
        transaction.commit();

    }

    private void setListener() {

        searchIv.setOnClickListener(onClickListener);
        typeLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setClickPos(position);
                changeRightPage(position);
            }
        });
    }


    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.frag_type_iv:
                    //获取用户输入
                    String queryStr = searchEt.getText().toString();

                    //查询信息

                    //将信息写入组件

                    break;

            }

        }
    };



    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        TypeBean typeBean = new Gson().fromJson(result,TypeBean.class);

        List<TypeBean.DataBean> list = typeBean.getData();
        data.addAll(list);
        adapter.notifyDataSetChanged();

    }

    private void initView(View view) {

        typeLv = view.findViewById(R.id.frag_type_lv);
        searchEt = view.findViewById(R.id.frag_type_et);
        searchIv = view.findViewById(R.id.frag_type_iv);



    }
}